from collections import OrderedDict

from django.contrib.auth.models import Group
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import BasePermission
from rest_framework.response import Response

from goods.models import SPUSpecification


def jwt_response_payload_handler(token, user=None, request=None):
    """
    自定义jwt认证成功返回数据
    """
    return {
        'token': token,
        'id': user.id,
        'username': user.username
    }

# 分页器
class PageNum(PageNumberPagination):
    page_size = 9
    page_size_query_param = 'pagesize'
    max_page_size = 100

    def get_paginated_response(self, data):
        return Response(OrderedDict([
            ('count', self.page.paginator.count),
            ('lists', data),
            ('page', self.page.number),
            ('pages', self.page.paginator.num_pages),
            ('pagesize', self.max_page_size),
        ]))
#规格权限
class SpecsPermission(BasePermission):

    def has_permission(self, request, view):
        app_name = 'goods'
        model_name = 'spuspecification'
        dict = {
            'update':'%s.change_%s' % (app_name,model_name),
            'create':'%s.add_%s' % (app_name,model_name),
            'destroy':'%s.delete_%s' % (app_name,model_name)
        }
        if request.user.has_perm(dict[view.action]):
            return True
        else:
            return False

#spu权限
class SpuPermission(BasePermission):

    def has_permission(self, request, view):
        app_name = 'goods'
        model_name = 'spu'
        dict = {
            'update':'%s.change_%s' % (app_name,model_name),
            'create':'%s.add_%s' % (app_name,model_name),
            'destroy':'%s.delete_%s' % (app_name,model_name)
        }
        if request.user.has_perm(dict[view.action]):
            return True
        else:
            return False

#sku权限
class SkuPermission(BasePermission):

    def has_permission(self, request, view):
        app_name = 'goods'
        model_name = 'sku'
        dict = {
            'update':'%s.change_%s' % (app_name,model_name),
            'create':'%s.add_%s' % (app_name,model_name),
            'destroy':'%s.delete_%s' % (app_name,model_name)
        }
        if request.user.has_perm(dict[view.action]):
            return True
        else:
            return False

#permission权限
class PerPermission(BasePermission):

    def has_permission(self, request, view):
        app_name = 'auth'
        model_name = 'permission'
        dict = {
            'update':'%s.change_%s' % (app_name,model_name),
            'create':'%s.add_%s' % (app_name,model_name),
            'destroy':'%s.delete_%s' % (app_name,model_name)
        }
        if request.user.has_perm(dict[view.action]):
            return True
        else:
            return False

#option权限
class OptionPermission(BasePermission):

    def has_permission(self, request, view):
        app_name = 'goods'
        model_name = 'specificationoption'
        dict = {
            'update':'%s.change_%s' % (app_name,model_name),
            'create':'%s.add_%s' % (app_name,model_name),
            'destroy':'%s.delete_%s' % (app_name,model_name)
        }
        if request.user.has_perm(dict[view.action]):
            return True
        else:
            return False

#image权限
class ImagePermission(BasePermission):

    def has_permission(self, request, view):
        app_name = 'goods'
        model_name = 'skuimage'
        dict = {
            'update':'%s.change_%s' % (app_name,model_name),
            'create':'%s.add_%s' % (app_name,model_name),
            'destroy':'%s.delete_%s' % (app_name,model_name)
        }
        if request.user.has_perm(dict[view.action]):
            return True
        else:
            return False

#group权限
class GroupPermission(BasePermission):

    def has_permission(self, request, view):
        app_name = 'auth'
        model_name = 'group'
        dict = {
            'update':'%s.change_%s' % (app_name,model_name),
            'create':'%s.add_%s' % (app_name,model_name),
            'destroy':'%s.delete_%s' % (app_name,model_name)
        }
        if request.user.has_perm(dict[view.action]):
            return True
        else:
            return False

#channel权限
class ChannelPermission(BasePermission):

    def has_permission(self, request, view):
        app_name = 'goods'
        model_name = 'goodschannel'
        dict = {
            'update':'%s.change_%s' % (app_name,model_name),
            'create':'%s.add_%s' % (app_name,model_name),
            'destroy':'%s.delete_%s' % (app_name,model_name)
        }
        if request.user.has_perm(dict[view.action]):
            return True
        else:
            return False

#brand权限
class BrandPermission(BasePermission):

    def has_permission(self, request, view):
        app_name = 'goods'
        model_name = 'brand'
        dict = {
            'update':'%s.change_%s' % (app_name,model_name),
            'create':'%s.add_%s' % (app_name,model_name),
            'destroy':'%s.delete_%s' % (app_name,model_name)
        }
        if request.user.has_perm(dict[view.action]):
            return True
        else:
            return False

#admin权限
class AdminPermission(BasePermission):

    def has_permission(self, request, view):
        app_name = 'users'
        model_name = 'user'
        dict = {
            'update':'%s.change_%s' % (app_name,model_name),
            'create':'%s.add_%s' % (app_name,model_name),
            'destroy':'%s.delete_%s' % (app_name,model_name)
        }
        if request.user.has_perm(dict[view.action]):
            return True
        else:
            return False

#user权限
class UserPermission(BasePermission):

    def has_permission(self, request, view):
        app_name = 'users'
        model_name = 'user'
        dict = {
            'PUT':'%s.change_%s' % (app_name,model_name),
            'POST':'%s.add_%s' % (app_name,model_name),
            'DELETE':'%s.delete_%s' % (app_name,model_name)
        }
        if request.method == 'GET' or request.user.has_perm(dict[request.method]):
            return True
        else:
            return False

